// 动态水生成
function WaterPrimitive(options) {
    this._positions = options.positions;
    this._aper = options.aper;
    this._speedVal = options.speedVal
    this._height = options.height;
    this._textureCoordinates = options.textureCoordinates;
    this._extrudedHeight = options.extrudedHeight;
    this._targetHeight = Cesium.defaultValue(options.targetHeight, options.extrudedHeight);
    this._grow = Cesium.defaultValue(options.grow, false);
    this._upGrow = Cesium.defaultValue(options.upGrow, true);
    this._isAnimating = false; // 新增：标记是否正在动画中
    this.init();
}
Object.defineProperties(WaterPrimitive.prototype, {
    extrudedHeight: {
        get() {
            return this._extrudedHeight;
        },
        set(newVal) {
            // if (Object.prototype.toString.call(newVal) !== "[object Number]") return;
            if (typeof newVal !== 'number') return;
            if (this._primitive) {
                this._primitive._state = 3;
                this._primitive._appearance = undefined;
                this._primitive.geometryInstances.geometry = this.getGeometry();
                this._extrudedHeight = newVal;
            }
        }
    }
});
WaterPrimitive.prototype.getGeometry = function () {
    return new Cesium.PolygonGeometry({
        polygonHierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(this._positions)),
        height: this._height,
        extrudedHeight: this._extrudedHeight,
        textureCoordinates: this._textureCoordinates,
        vertexFormat: Cesium.MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat
    });
};
WaterPrimitive.prototype.update = function (frameState) {
    if (this._primitive) {
        let primitive = this._primitive;
        primitive.update(frameState);
    }
};

WaterPrimitive.prototype.destroy = function () {
    let primitive = this._primitive;
    primitive.destroy()
}

WaterPrimitive.prototype.init = function () {
    let geometry = this.getGeometry();
    if (!geometry) return;
    this._primitive = new Cesium.Primitive({
        releaseGeometryInstances: false,
        geometryInstances: new Cesium.GeometryInstance({
            geometry
        }),
        asynchronous: false,
        appearance: this._aper
    });
    if (this._grow) {
        this.grow();
    }
};
WaterPrimitive.prototype.grow = function () {
    // if (this._upGrow) {
    //     if (this.extrudedHeight < this._targetHeight) {
    //         this.extrudedHeight += this._speedVal;
    //         requestAnimationFrame(() => this.grow());
    //     }
    // } else {
    //     if (this.extrudedHeight > this._targetHeight) {
    //         this.extrudedHeight -= this._speedVal;
    //         requestAnimationFrame(() => this.grow());
    //     }
    // }


    //水位上涨
    // this._isAnimating = true;

    // if (this._upGrow) {
    //     if (this.extrudedHeight < this._targetHeight) {
    //         this.extrudedHeight += this._speedVal;
    //         requestAnimationFrame(() => this.grow());
    //     } else {
    //         this._isAnimating = false;
    //     }
    // } else {
    //     if (this.extrudedHeight > this._targetHeight) {
    //         this.extrudedHeight -= this._speedVal;
    //         requestAnimationFrame(() => this.grow());
    //     } else {
    //         this._isAnimating = false;
    //     }
    // }


    // 如果不需要变化或已经达到目标，则停止
    if (!this._grow) return;

    if (this._upGrow) {
        // 上涨逻辑
        if (this.extrudedHeight < this._targetHeight) {
            this.extrudedHeight = Math.min(this.extrudedHeight + this._speedVal, this._targetHeight);
            requestAnimationFrame(() => this.grow());
        } else {
            this._grow = false; // 达到目标，停止动画
        }
    } else {
        // 下降逻辑
        if (this.extrudedHeight > this._targetHeight) {
            this.extrudedHeight = Math.max(this.extrudedHeight - this._speedVal, this._targetHeight);
            requestAnimationFrame(() => this.grow());
        } else {
            this._grow = false; // 达到目标，停止动画
        }
    }
}
// 添加设置目标高度的方法
WaterPrimitive.prototype.setTargetHeight = function (newHeight) {
    // this._targetHeight = newHeight;
    // if (!this._isAnimating) {
    //     this._grow = true;
    //     this.grow();
    // }

    if (typeof newHeight !== 'number') {
        console.warn('水位高度必须是数字');
        return;
    }

    // 判断是上涨还是下降
    this._upGrow = newHeight > this._extrudedHeight;
    this._targetHeight = newHeight;
    this._grow = true;

    // 开始水位变化动画
    this.grow();
};

Cesium.WaterPrimitive = WaterPrimitive;